package com.dupt.common.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;

/**
 * 钉钉发送消息
 *
 * @author zhuanyu.sun
 * @date 2025-06-11
 */
public class SendMessage {
    /**
     * 使用 Token 初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkcrm_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkcrm_1_0.Client(config);
    }

    /**
     * 发送钉钉消息通知
     * @return Client
     * @throws Exception
     */
    public static void sendAllMessage(String type,String userId,String title,String message,String messageUrl) throws Exception {

        try {
            String accessToken = AccessToken.getAccessToken();
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
            request.setAgentId(3891775968L);
            request.setUseridList(userId);
            request.setToAllUser(false);

            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            if("text".equals(type)){
                msg.setMsgtype(type);
                msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
                msg.getText().setContent(message);
            }else if("image".equals(type)){
                msg.setMsgtype(type);
                msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
                msg.getImage().setMediaId("@lADOdvRYes0CbM0CbA");
            }else if("file".equals(type)){
                msg.setMsgtype(type);
                msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
                msg.getFile().setMediaId("@lADOdvRYes0CbM0CbA");
            }else if("link".equals(type)){
                msg.setMsgtype(type);
                msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
                msg.getLink().setTitle(title);
                msg.getLink().setText(message);
                msg.getLink().setMessageUrl(messageUrl);
                //msg.getLink().setPicUrl(MediaUpload.getMedia(accessToken));
                msg.getLink().setPicUrl("@lALPDfmVdnru5CLMyMzI");
            }else if("markdown".equals(type)){
                msg.setMsgtype(type);
                msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
                msg.getMarkdown().setText(message);
                msg.getMarkdown().setTitle(title);
            }else if("oa".equals(type)){
                msg.setMsgtype(type);
                msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
                msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
                msg.getOa().getHead().setText(title);
                msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
                msg.getOa().getBody().setContent(message);
            }else if("action_card".equals(type)){
                msg.setMsgtype(type);
                msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
                msg.getActionCard().setTitle(title);
                msg.getActionCard().setMarkdown(message);
                msg.getActionCard().setSingleTitle(title);
                msg.getActionCard().setSingleUrl(messageUrl);
            }
            request.setMsg(msg);
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
